
package com.achievement.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.achievement.domain.dto.AppraisalProductAuthorDTO;
import com.achievement.domain.entity.AppraisalProductAuthorEntity;
import com.achievement.domain.vo.AppraisalProductAuthorVO;
import com.achievement.mapper.AppraisalProductAuthorMapper;
import com.achievement.mappings.AppraisalProductMapping;
import com.achievement.service.IAppraisalProductAuthorService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * AppraisalProductAuthorServiceImpl
 */

@Service
public class AppraisalProductAuthorServiceImpl extends ServiceImpl<AppraisalProductAuthorMapper, AppraisalProductAuthorEntity>
        implements IAppraisalProductAuthorService {

    @Autowired
    private AppraisalProductMapping mapping;


    /**
     * 批量删除科研成果-鉴定成果列-作者
     *
     * @param ids 需要删除的数据主键集合
     * @return 结果
     */
    @Override
    public boolean deleteByAppraisalProductIds(List<String> ids) {
        if (CollUtil.isEmpty(ids)){
            return true;
        }

        //根据researchReportId，删除作者信息
        LambdaQueryWrapper<AppraisalProductAuthorEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(AppraisalProductAuthorEntity::getAppraisalProductId,ids);

        return this.baseMapper.delete(queryWrapper)>0;
    }

    /**
     * 批量删除科研成果-鉴定成果列-作者
     *
     * @param uuid 需要删除的数据主键集合
     * @return 结果
     */
    @Override
    public boolean deleteByAppraisalProductId(String uuid) {
        //根据researchReportId，删除作者信息
        LambdaQueryWrapper<AppraisalProductAuthorEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AppraisalProductAuthorEntity::getAppraisalProductId,uuid);

        return this.baseMapper.delete(queryWrapper)>0;
    }

    /**
     * 新增作者
     *
     * @param authorList
     */
    @Override
    public boolean add(List<AppraisalProductAuthorDTO> authorList) {
        List<AppraisalProductAuthorEntity> entityList =
                authorList.stream().map(mapping::appraisalDto2AppraisalEntity).collect(Collectors.toList());

        if (CollUtil.isNotEmpty(entityList)){
            return this.saveBatch(entityList);
        }

        return true;
    }

    /**
     * 根据鉴定成果id，查询相关作者信息
     *
     * @param  id id
     */
    @Override
    public List<AppraisalProductAuthorVO> getInfoByAppraisalProductId(String id) {
        LambdaQueryWrapper<AppraisalProductAuthorEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AppraisalProductAuthorEntity::getAppraisalProductId,id);
        queryWrapper.orderByAsc(AppraisalProductAuthorEntity::getOrderId);

        List<AppraisalProductAuthorEntity> list = this.list(queryWrapper);

        List<AppraisalProductAuthorVO> voList = list.stream().map(mapping::authorEntity2Vo).collect(Collectors.toList());
        return voList;
    }
}
